<?php

/**
 * Created by PhpStorm.
 * Script Name: EventAuthorizedHandler.php
 * Create: 2020/4/15 10:54
 * Description: 快速注册小程序处理器
 * Author: fudaoji<fdj@kuryun.cn>
 */
namespace plugin\wechat\app\platform\handler\platform;

use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use plugin\wechat\app\service\Mini as MiniService;
use plugin\wechat\app\service\MiniReg as RegService;


class EventThirdFastRegisteredHandler implements EventHandlerInterface
{

    /**
     * 处理器
     * @param mixed $payload
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author fudaoji<fdj@kuryun.cn>
     */
    public function handle($payload = null) {
        
        /**
         * 成功： <xml>
    <AppId><![CDATA[第三方平台appid]]></AppId>
    <CreateTime>1535442403</CreateTime>
    <InfoType><![CDATA[notify_third_fasteregister]]></InfoType>
    <appid>创建小程序appid</appid>
    <status>0</status>
    <auth_code>xxxxx第三方授权码</auth_code>
    <msg>OK</msg>
    <info>
    <name><![CDATA[企业名称]]></name>
    <code><![CDATA[企业代码]]></code>
    <code_type>1</code_type>
    <legal_persona_wechat><![CDATA[法人微信号]]></legal_persona_wechat>
    <legal_persona_name><![CDATA[法人姓名]]></legal_persona_name>
    <component_phone><![CDATA[第三方联系电话]]></component_phone>
    </info>
</xml>

失败：{"AppId":"wx7ec884d7ea3bb23b","CreateTime":"1702459396","InfoType":"notify_third_fasteregister","status":"1007","msg":"id card exceed register limits","info":{"name":"","code":"","code_type":"1","legal_persona_wechat":"","legal_persona_name":"","component_phone":""}}
         * 
         */
        
        dao_log()->error('注册事件：'.json_encode($payload, JSON_UNESCAPED_UNICODE));
        
        $update = [
            'status' => 0
        ];
        if(isset($payload['status']) && $payload['status'] == 0){
            $update['status'] = 2;
            $update['appid'] = $payload['appid'];
            $map = [
                'code' => $payload['code'],
                'legal_persona_wechat' => $payload['legal_persona_wechat'],
                'legal_persona_name' => $payload['legal_persona_name']
            ];
        }else{
            $info = $payload['info'];
            $map = [
                'code' => $info['code'],
                'legal_persona_wechat' => $info['legal_persona_wechat'],
                'legal_persona_name' => $info['legal_persona_name']
            ];
        }
        
        RegService::updateByMap($update, $map);
    }
}